package com.newmainsoftech.aspectjutil.eventmanager;

import java.lang.reflect.Method;

import junit.framework.Assert;

import org.junit.Test;
import org.slf4j.LoggerFactory;

public class EventHandlerTestWithChildEventRecipient2 extends EventHandlerTestTemplate {
	static {
		logger = LoggerFactory.getLogger( EventHandlerTestWithChildEventRecipient2.class);
	}
	
	// Tests for @EventListener method inheritance ------------------------------------------------
	@Test
	public void testEventRecipientMethodInheritance() throws Throwable {
		// Instantiate ChildEventRecipient2 object --------------------------------------------
		Object childEventRecipient2Obj 
		= EventManagerTestUtils.childEventRecipient2Factory( childDelegationClassLoader);
		// ----------------------------------------------------------------------------------------
		
		// Execute EventProducer.calculate method -------------------------------------------------
		Object eventProducerObj = EventManagerTestUtils.eventProducerFactory( childDelegationClassLoader);
		Method calculateMethod 
		= EventManagerTestUtils.getEventProducerCalculateMethod( eventProducerObj.getClass());
		Integer calculationResult = (Integer)calculateMethod.invoke( eventProducerObj, 4, 8);
		// ----------------------------------------------------------------------------------------
		
		// Verifications --------------------------------------------------------------------------
		Assert.assertEquals( 
				EventManagerTestUtils.getEventTraceListDump( eventTraceList), 1, eventTraceList.size());
			Assert.assertTrue( 
					EventManagerTestUtils.getEventTraceListDump( eventTraceList), 
					eventTraceList.get( 0)
					.contains( 
							EventManagerTestUtils.EventRecipientClassName 
							+ ".staticListenerOfCalculationCompletion"
							) 
					);
				int eventTraceIndex = 0;
				Assert.assertEquals( 
						EventManagerTestUtils.getCalculationEventClass( childDelegationClassLoader),
						triggeredEventTraceList.get( eventTraceIndex)
						);
				Class<?> eventerInfoClass 
				= EventManagerTestUtils.getEventerInfoClass( childDelegationClassLoader);
				Object eventerInfo = eventerInfoClass.cast( eventerTraceList.get( eventTraceIndex));
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerObjOfEventerInfo( eventerInfo),
							eventProducerObj
							);
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerMethodOfEventerInfo( eventerInfo).getName(),
							"calculate"
							);
				Assert.assertEquals( 
						(Integer)eventResultTraceList.get( eventTraceIndex), 
						calculationResult
						);
		// ----------------------------------------------------------------------------------------
			
		eventTraceList.clear();
		triggeredEventTraceList.clear();
		eventerTraceList.clear();
		eventResultTraceList.clear();
		
		// Execute ChildEventProducer3.calculate method -------------------------------------------
		Object childEventProducer3Obj 
		= EventManagerTestUtils.childEventProducer3Factory( childDelegationClassLoader);
		calculateMethod 
		= EventManagerTestUtils.getEventProducerCalculateMethod( childEventProducer3Obj.getClass());
		calculationResult = (Integer)calculateMethod.invoke( childEventProducer3Obj, 4, 8);
		// ----------------------------------------------------------------------------------------
		
		// Verifications --------------------------------------------------------------------------
		Assert.assertEquals( 
				EventManagerTestUtils.getEventTraceListDump( eventTraceList) + "\n", 
				1, 
				eventTraceList.size());
			Assert.assertTrue( 
					EventManagerTestUtils.getEventTraceListDump( eventTraceList), 
					eventTraceList.get( 0)
					.contains( 
							EventManagerTestUtils.ChildEventRecipient2ClassName 
							+ ".onCalculationCompletion"
							) 
					);
				eventTraceIndex = 0;
				Assert.assertEquals( 
						EventManagerTestUtils.getOverridenCalculationEventClass( childDelegationClassLoader),
						triggeredEventTraceList.get( eventTraceIndex)
						);
				eventerInfoClass 
				= EventManagerTestUtils.getEventerInfoClass( childDelegationClassLoader);
				eventerInfo = eventerInfoClass.cast( eventerTraceList.get( eventTraceIndex));
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerObjOfEventerInfo( eventerInfo),
							childEventProducer3Obj
							);
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerMethodOfEventerInfo( eventerInfo).getName(),
							"calculate"
							);
				Assert.assertEquals( 
						(Integer)eventResultTraceList.get( eventTraceIndex), 
						calculationResult
						);
		// ----------------------------------------------------------------------------------------
	} // public void testEventRecipientMethodInheritance()
	// --------------------------------------------------------------------------------------------
}
